package com.vooda.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * Created by kenny
 * on 2017/8/16.
 */
public class TimeUtil {
    private static final long TWOTIME = 2 * 24 * 60 * 60 * 1000;//两天时间
    private static final long ONETIME = 24 * 60 * 60 * 1000;//一天时间
    private static final long SIXHOURSTIME = 6 * 60 * 60 * 1000;//6小时时间
    private static final long ONEHOURSTIME = 60 * 60 * 1000;//一小时时间

    public static String hasTime(long ld) {
        ld = ld * 1000;
        Date date = new Date(ld);
//标准日历系统类
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(date);
//java.text.SimpleDateFormat，设置时间格式
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//得到毫秒值转化的时间
        String time = format.format(gc.getTime());
        return time;
    }

    public static String hasTimeShiFen(long ld) {
//        ld = ld * 1000;
        Date date = new Date(ld);
//标准日历系统类
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(date);
//java.text.SimpleDateFormat，设置时间格式
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
//得到毫秒值转化的时间
        String time = format.format(gc.getTime());
        return time;
    }

    //求当前时间到本天结束的时间
    public static long hasLastTime(long s) throws ParseException {
        s = s * 1000;
        Date date = new Date(s);
//标准日历系统类
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(date);
//java.text.SimpleDateFormat，设置时间格式
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//得到毫秒值转化的时间
        String time = format.format(gc.getTime());
        String[] re = time.split(" ");
        String ss = re[0] + " 23:59:59";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//得到毫秒数
        long timeStart = sdf.parse(ss).getTime();

        return timeStart - s;
    }

    public static String setTimeType(long time) {
        long l = System.currentTimeMillis();
        long timelast = 0;
        try {
            timelast = TimeUtil.hasLastTime(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
//        long m = time * 1000;
        long m = time;
        if (l - m >= TWOTIME) {
            return hasTimeShiFen(time);
        } else if (l - m >= timelast) {
            return hasTimeShiFen(time);
        } else if (l - m <= ONETIME && l - m > SIXHOURSTIME) {

            String times = TimeUtil.hasTimeShiFen(time);
            String[] re = times.split(" ");
            return re[1];
        } else if (l - m <= SIXHOURSTIME) {

            long j = l - m;
            if (j / ONEHOURSTIME >= 1) {
                return j / ONEHOURSTIME + "小时前";
            } else if (j > 60000 && j < ONEHOURSTIME) {
                double b = j * 1.0f / ONEHOURSTIME;
                int i = (int) (b * 60);
                return i + "分钟前";
            } else {
                return "刚刚";
            }

        } else {
            return hasTimeShiFen(time);
        }
    }
}
